package com.roadmap.ajax.proxy;

import com.roadmap.ajax.ProgressStatus;
import com.roadmap.biz.common.IManager;
import com.roadmap.biz.common.ManagerFactory;
import com.roadmap.common.RdcConst;
import com.roadmap.common.RdcErrorMessage;
import com.roadmap.common.RdcResourceBundle;
import com.roadmap.entities.Category;
import com.roadmap.entities.Comment;
import com.roadmap.entities.Execution;
import com.roadmap.entities.Friendship;
import com.roadmap.entities.LifeGoal;
import com.roadmap.entities.Message;
import com.roadmap.entities.Request;
import com.roadmap.entities.ext.GoalExt;
import com.roadmap.entities.ext.PlanExt;
import com.roadmap.entities.ext.TaskExt;

/**
 * 向client提供Ajax接口
 */
public class AjaxProxy extends BaseProxy {

	public AjaxProxy() {
	}

	// 好友请求
	public Object createRequest(Request req) {
		if (req == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}

		req.setId(getUIDInSession());
		return create(req);
	}

	public Object completeRequest(String reqId) {
		if (reqId == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}
		return complete(RdcConst.REQUEST, reqId);
	}

	public Object skipRequest(String reqId) {
		if (reqId == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}
		return cancel(RdcConst.REQUEST, reqId);
	}

	// 好友
	public Object removeFriend(String friendId) {
		if (friendId == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}
		return delete(RdcConst.FRIEND, friendId);
	}

	public Object addFriendGroup(Category group) {
		if (group == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}
		group.setEntryType(Friendship.class.getSimpleName());
		group.setOwnerId(getUIDInSession());
		return create(group);
	}

	// 消息
	public Object createMessage(String dialogId, String content) {
		if (dialogId == null || content == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}

		Message msg = new Message();
		msg.setDialogId(dialogId);
		msg.setContent(content);
		msg.setDispatcherId(getUIDInSession());
		return create(msg);
	}

	// 评论
	public Object createComment(Comment cmt) {
		return create(cmt);
	}

	public Object editComment(Comment cmt) {
		return update(cmt);
	}

	public Object deleteComment(Comment cmt) {
		return delete(RdcConst.COMMENT, cmt.getId());
	}

	// 目标
	public Object saveGoal(LifeGoal goal) {
		if (goal == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}

		return (goal.getId() == null) ? create(goal) : update(goal);
	}

	public Object completeGoal(String goalId) {
		return complete(RdcConst.GOAL, goalId);
	}

	public Object cancelGoal(String goalId) {
		return cancel(RdcConst.GOAL, goalId);
	}

	public Object deleteGoal(String goalId) {
		if (goalId == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}

		return delete(RdcConst.GOAL, goalId);
	}

	// 执行记录的操作
	public Object createExec(Execution exec) {
		Object entry = create(exec);
		if (!(entry instanceof Execution)) {
			return entry;
		}

		ProgressStatus progress = new ProgressStatus();
		String currentUser = this.getUIDInSession();
		try {
			// 获得任务的状态
			IManager taskMgr = ManagerFactory.getInstance(RdcConst.TASK,
					currentUser);
			TaskExt task = (TaskExt) taskMgr.getEntityExt(exec.getTaskId());
			progress.setTaskId(task.getId());
			progress
					.setTaskStatus(RdcResourceBundle.LIFETASK_STATUS_DESCRIPTIONS[task
							.getStatus()]);
			progress.setTaskProgress(task.getProgress());
			progress.setTaskSymptom(task.getSymptom());

			// 获得目标的状态
			if (task.getGoalId() != null) {
				IManager mgr = ManagerFactory.getInstance(RdcConst.GOAL,
						currentUser);
				GoalExt goal = (GoalExt) mgr.getEntityExt(task.getGoalId());
				progress.setGoalId(goal.getId());
				progress.setGoalProgress(goal.getProgress());
				progress
						.setGoalStatus(RdcResourceBundle.LIFEGOAL_STATUS_DESCRIPTIONS[task
								.getStatus()]);
				progress.setGoalSymptom(goal.getSymptom());
			}

			return progress;
		}
		catch (Exception e) {
			return RdcErrorMessage.getMessageToClient(e);
		}
	}

	public Object saveExec(Execution exec) {
		if (exec == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}

		return (exec.getId() == null) ? create(exec) : update(exec);
	}

	public Object deleteExec(String execId) {
		if (execId == null) {
			return RdcErrorMessage.BAD_PARAMETERS;
		}

		return delete(RdcConst.EXEC, execId);
	}

}
